﻿Imports System.Drawing.Printing

Public Class AccountSumPrintDocument
    Inherits ReportPrintDocument


    ''打印结束通知外部
    'Public Delegate Sub EndPrintCallBack(ByVal sender As Object, ByVal e As PrintCallBackEventArgs)
    'Private m_EndCallBack As EndPrintCallBack

    '打印数据源
    Private m_AcctSumList As List(Of ClsTAccountSum)

    '公司信息
    Private m_CompanyName As String

    ''页面控制
    ''页边距设置
    'Private m_LeftMargin As Integer
    'Private m_RightMargin As Integer
    'Private m_TopMargin As Integer
    'Private m_BottomMargin As Integer

    ''页数
    'Private _TotalPages As Integer
    'Private _CurrentPage As Integer
    ''单页行数
    'Private _RowsOfPage As Integer '每页行数
    'Private _CurrentItemIndex As Integer '当前正在打印的记录索引

    ''每页的绘制点
    'Private _CurrentPoint As Point

    '字体设置
    Private m_TitleFont As Font '= New Font("宋体", 14, FontStyle.Bold)
    Private m_HeaderFont As Font ' = New Font("宋体", 9, FontStyle.Bold)
    Private m_FooterFont As Font '=  New Font("宋体", 8, FontStyle.Regular)
    Private m_TextNormalFont As Font '= New Font("宋体", 8, FontStyle.Regular)
    Private m_TextBoldFont As Font '= New Font("宋体", 8, FontStyle.Bold)
    Private m_TextSmallFont As Font '= New Font("宋体", 6, FontStyle.Regular)

    '笔刷设置
    Private m_Brush As SolidBrush ' = New SolidBrush(Color.Black)
    Private m_Pen As Pen '= New Pen(New SolidBrush(Color.Black))

    ''分辨率设定
    'Private _dpiX As Single = 96
    'Private _dpiY As Single = 96

    '各部位高度与宽度
    '高度
    Private m_PageHeaderHeight As Single '页眉高度， 科目汇总 大标题 1.4cm
    Private m_WorkPeriodHeight As Single '会计期间高度 0.6cm
    Private m_CompanyHeight As Single '核算单位高度 0.8cm
    Private m_ItemHeaderHeight As Single '表头行高度 0.8cm
    Private m_ItemHeight As Single '表格行高度 0.7cm
    Private m_ItemFooterHeight As Single '表尾行高度 0.8cm
    Private m_PageFooterHeight As Single '页脚高度 0.9cm
    '宽度
    Private m_AcctCodeWidth As Single '科目编码区宽度 2cm
    Private m_AcctNameWidth As Single '科目名称区宽度 3cm
    Private m_DebitWidth As Single '借方宽度 2.8cm
    Private m_CreditWidth As Single '贷方宽度 2.8cm

    'Private m_PageWidth As Single '页面总体打印宽度 11cm
    'Private m_RowWidth As Single '一行数据的总宽度 10.6cm

    '设定文字部分
    Private m_PageTitle As String = "科目汇总表"
    Private m_AcctCodeTitle As String = "科目编码"
    Private m_AcctNameTitle As String = "科目名称"
    Private m_DebitSumTitle As String = "借方汇总"
    Private m_CreditSumTitle As String = "贷方汇总"

    '合计部分的数值
    Private m_DebitSum As Decimal '借方汇总值 
    Private m_CreditSum As Decimal '贷方汇总值

    '默认纸张
    'Private m_DefaultPageSize As Printing.PaperSize

#Region "构造函数"

    ''' <summary>
    ''' 构造函数
    ''' </summary>
    Public Sub New()
        MyBase.New()
        '初始化打印值
        ''页边距设置
        'm_LeftMargin = 10
        'm_RightMargin = 10
        'm_TopMargin = 20
        'm_BottomMargin = 20

        '页数
        _TotalPages = 0
        _CurrentPage = 0

        '单页行数
        _RowsOfPage = 6

        '凭证标题名称

        '字体设置
        m_TitleFont = New Font("宋体", 14, FontStyle.Bold)
        m_HeaderFont = New Font("宋体", 9, FontStyle.Bold)
        m_FooterFont = New Font("宋体", 8, FontStyle.Regular)
        m_TextNormalFont = New Font("宋体", 8, FontStyle.Regular)
        m_TextBoldFont = New Font("宋体", 8, FontStyle.Bold)
        m_TextSmallFont = New Font("宋体", 6, FontStyle.Regular)

        '笔刷设置
        m_Brush = New SolidBrush(Color.Black)
        m_Pen = New Pen(New SolidBrush(Color.Black))

        '默认纸张大小
        DefaultPageSize = New Printing.PaperSize("自定义凭证纸", 492, 846)

        '单页高度设置,单位为厘米 英寸
        '_dpiX = 600
        '_dpiY = 600

        '宽度设置:厘米 * 567 = twips / 1440 = 英寸 * _dpiX =像素
        m_AcctCodeWidth = 2 / 2.54 * _dpiX
        m_AcctNameWidth = 3 / 2.54 * _dpiX
        m_DebitWidth = 2.8 / 2.54 * _dpiX
        m_CreditWidth = 2.8 / 2.54 * _dpiX

        PageWidth = 11 / 2.54 * _dpiX
        RowWidth = 10.6 / 2.54 * _dpiX

        '高度设置
        m_PageHeaderHeight = 1.4 / 2.54 * _dpiY '页眉高度， 科目汇总 大标题 1.4cm
        m_WorkPeriodHeight = 0.6 / 2.54 * _dpiY  '会计期间高度 0.6cm
        m_CompanyHeight = 0.8 / 2.54 * _dpiY  '核算单位高度 0.8cm
        m_ItemHeaderHeight = 0.8 / 2.54 * _dpiY  '表头行高度 0.8cm
        m_ItemHeight = 0.7 / 2.54 * _dpiY  '表格行高度 0.7cm
        m_ItemFooterHeight = 0.8 / 2.54 * _dpiY  '表尾行高度 0.8cm
        m_PageFooterHeight = 0.9 / 2.54 * _dpiY  '页脚高度 0.9cm

    End Sub

#End Region

#Region "属性区"
    'Public Property AcctSumList As List(Of ClsTAccountSum)
    '    Get
    '        Return m_AcctSumList
    '    End Get
    '    Set(value As List(Of ClsTAccountSum))

    '        m_AcctSumList = value

    '        '重新计算分页
    '        StatisticsPages()
    '    End Set
    'End Property

    'Public Property LeftMargin As Integer
    '    Get
    '        Return m_LeftMargin
    '    End Get
    '    Set(value As Integer)
    '        m_LeftMargin = value
    '    End Set
    'End Property

    'Public Property RightMargin As Integer
    '    Get
    '        Return m_RightMargin
    '    End Get
    '    Set(value As Integer)
    '        m_RightMargin = value
    '    End Set
    'End Property

    'Public Property TopMargin As Integer
    '    Get
    '        Return m_TopMargin
    '    End Get
    '    Set(value As Integer)
    '        m_TopMargin = value
    '    End Set
    'End Property

    'Public Property BottomMargin As Integer
    '    Get
    '        Return m_BottomMargin
    '    End Get
    '    Set(value As Integer)
    '        m_BottomMargin = value
    '    End Set
    'End Property

    Public Property TitleFont As Font
        Get
            Return m_TitleFont
        End Get
        Set(value As Font)
            m_TitleFont = value
        End Set
    End Property

    Public Property HeaderFont As Font
        Get
            Return m_HeaderFont
        End Get
        Set(value As Font)
            m_HeaderFont = value
        End Set
    End Property

    Public Property TextNormalFont As Font
        Get
            Return m_TextNormalFont
        End Get
        Set(value As Font)
            m_TextNormalFont = value
        End Set
    End Property

    Public Property TextSmallFont As Font
        Get
            Return m_TextSmallFont
        End Get
        Set(value As Font)
            m_TextSmallFont = value
        End Set
    End Property

    Public Property Brush As SolidBrush
        Get
            Return m_Brush
        End Get
        Set(value As SolidBrush)
            m_Brush = value
        End Set
    End Property

    Public Property Pen As Pen
        Get
            Return m_Pen
        End Get
        Set(value As Pen)
            m_Pen = value
        End Set
    End Property

    'Public Property EndCallBack As EndPrintCallBack
    '    Get
    '        Return m_EndCallBack
    '    End Get
    '    Set(value As EndPrintCallBack)
    '        m_EndCallBack = value
    '    End Set
    'End Property

    Public Property CompanyName As String
        Get
            Return m_CompanyName
        End Get
        Set(value As String)
            m_CompanyName = value
        End Set
    End Property

    Public Property PageHeaderHeight As Single
        Get
            Return m_PageHeaderHeight
        End Get
        Set(value As Single)
            m_PageHeaderHeight = value
        End Set
    End Property

    Public Property WorkPeriodHeight As Single
        Get
            Return m_WorkPeriodHeight
        End Get
        Set(value As Single)
            m_WorkPeriodHeight = value
        End Set
    End Property

    Public Property CompanyHeight As Single
        Get
            Return m_CompanyHeight
        End Get
        Set(value As Single)
            m_CompanyHeight = value
        End Set
    End Property

    Public Property ItemHeaderHeight As Single
        Get
            Return m_ItemHeaderHeight
        End Get
        Set(value As Single)
            m_ItemHeaderHeight = value
        End Set
    End Property

    Public Property ItemHeight As Single
        Get
            Return m_ItemHeight
        End Get
        Set(value As Single)
            m_ItemHeight = value
        End Set
    End Property

    Public Property ItemFooterHeight As Single
        Get
            Return m_ItemFooterHeight
        End Get
        Set(value As Single)
            m_ItemFooterHeight = value
        End Set
    End Property

    Public Property PageFooterHeight As Single
        Get
            Return m_PageFooterHeight
        End Get
        Set(value As Single)
            m_PageFooterHeight = value
        End Set
    End Property

    Public Property AcctCodeWidth As Single
        Get
            Return m_AcctCodeWidth
        End Get
        Set(value As Single)
            m_AcctCodeWidth = value
        End Set
    End Property

    Public Property AcctNameWidth As Single
        Get
            Return m_AcctNameWidth
        End Get
        Set(value As Single)
            m_AcctNameWidth = value
        End Set
    End Property

    Public Property DebitWidth As Single
        Get
            Return m_DebitWidth
        End Get
        Set(value As Single)
            m_DebitWidth = value
        End Set
    End Property

    Public Property CreditWidth As Single
        Get
            Return m_CreditWidth
        End Get
        Set(value As Single)
            m_CreditWidth = value
        End Set
    End Property

    Public Property PageTitle As String
        Get
            Return m_PageTitle
        End Get
        Set(value As String)
            m_PageTitle = value
        End Set
    End Property

    Public Property AcctCodeTitle As String
        Get
            Return m_AcctCodeTitle
        End Get
        Set(value As String)
            m_AcctCodeTitle = value
        End Set
    End Property

    Public Property AcctNameTitle As String
        Get
            Return m_AcctNameTitle
        End Get
        Set(value As String)
            m_AcctNameTitle = value
        End Set
    End Property

    Public Property DebitSumTitle As String
        Get
            Return m_DebitSumTitle
        End Get
        Set(value As String)
            m_DebitSumTitle = value
        End Set
    End Property

    Public Property CreditSumTitle As String
        Get
            Return m_CreditSumTitle
        End Get
        Set(value As String)
            m_CreditSumTitle = value
        End Set
    End Property

    'Public Property DefaultPageSize As PaperSize
    '    Get
    '        Return m_DefaultPageSize
    '    End Get
    '    Set(value As PaperSize)
    '        m_DefaultPageSize = value
    '    End Set
    'End Property

    Public Property DebitSum As Decimal
        Get
            Return m_DebitSum
        End Get
        Set(value As Decimal)
            m_DebitSum = value
        End Set
    End Property

    Public Property CreditSum As Decimal
        Get
            Return m_CreditSum
        End Get
        Set(value As Decimal)
            m_CreditSum = value
        End Set
    End Property

    Public Property FooterFont As Font
        Get
            Return m_FooterFont
        End Get
        Set(value As Font)
            m_FooterFont = value
        End Set
    End Property

    Public Property TextBoldFont As Font
        Get
            Return m_TextBoldFont
        End Get
        Set(value As Font)
            m_TextBoldFont = value
        End Set
    End Property

    Public Property AcctSumList As List(Of ClsTAccountSum)
        Get
            Return m_AcctSumList
        End Get
        Set(value As List(Of ClsTAccountSum))
            m_AcctSumList = value

            If value IsNot Nothing AndAlso value.Count > 0 Then
                StatisticsPages()
            End If
        End Set
    End Property

#End Region


#Region "统计分页 及 汇总"
    Protected Overrides Sub StatisticsPages()

        If m_AcctSumList Is Nothing OrElse CType(m_AcctSumList, List(Of ClsTAccountSum)).Count < 1 Then
            Return
        End If

        '总页数暂时等于1
        _TotalPages = 1

        '计算每页可打印多少行记录
        Dim curPagerHeight As Integer = DefaultPageSize.Height

        '每页可以打印行数
        _RowsOfPage = (curPagerHeight - m_PageHeaderHeight - m_PageFooterHeight - m_ItemHeaderHeight - m_ItemFooterHeight - m_WorkPeriodHeight - m_CompanyHeight) / m_ItemHeight
        '需要几页
        Dim tmpPages As Integer
        tmpPages = m_AcctSumList.Count \ _RowsOfPage '使用整除
        If (m_AcctSumList.Count Mod _RowsOfPage) > 0 Then
            tmpPages += 1
        End If

        _TotalPages = tmpPages

        '手工汇总
        CalculateSum()
    End Sub

    ''' <summary>
    ''' 对借贷发生额进行手工汇总
    ''' </summary>
    Private Sub CalculateSum()
        If m_AcctSumList Is Nothing OrElse m_AcctSumList.Count < 1 Then
            Return
        End If

        For Each itemSum As ClsTAccountSum In m_AcctSumList
            m_DebitSum += itemSum.DebitSum
            m_CreditSum += itemSum.CreditSum

        Next


    End Sub

#End Region

#Region "打印预览"
    '''' <summary>
    '''' 打印预览科目汇总，返回科目汇总报表
    '''' </summary>
    '''' <returns></returns>
    'Public Function PrintReport() As Printing.PrintDocument
    '    Dim printDoc As Printing.PrintDocument = New Printing.PrintDocument()
    '    '取出纸张设置
    '    If My.Settings.VoucherPageSettings IsNot Nothing Then
    '        printDoc.DefaultPageSettings = My.Settings.VoucherPageSettings
    '    End If
    '    '设置总页数
    '    printDoc.DocumentName = _TotalPages.ToString()

    '    AddHandler printDoc.PrintPage, AddressOf PrintAcctSumPage
    '    AddHandler printDoc.BeginPrint, AddressOf BeginPrint
    '    AddHandler printDoc.EndPrint, AddressOf EndPrintAcctSum

    '    Return printDoc

    'End Function
#End Region

#Region "打印事件"
    'Private Sub PrintAcctSumPage(ByVal sender As Object, ByVal e As Printing.PrintPageEventArgs)

    '    'Debug.Print("当前打印页：" & _CurrentPage)

    '    '当前页数累加,当前页数从1算起
    '    _CurrentPage += 1

    '    '当前绘制起始点
    '    _CurrentPoint = New Point(LeftMargin, TopMargin)

    '    '画页标题
    '    DrawPageTitle(e)
    '    '画会计期间
    '    DrawWorkPeriod(e)
    '    '画公司名称
    '    DrawCompany(e)

    '    '如果是第一页，则记录索引为0
    '    _CurrentItemIndex = (_CurrentPage - 1) * _RowsOfPage

    '    '画表头：
    '    DrawItemHeader(e)
    '    '画表格体：
    '    DrawItems(e)
    '    '画合计：
    '    DrawItemFooter(e)
    '    '画凭证脚：人员信息
    '    DrawPageFooter(e)

    '    '判断是否是最后一页
    '    '当前打印的是第几页？
    '    If _CurrentPage >= _TotalPages Then
    '        '最后一页
    '        '设置打印完毕
    '        e.HasMorePages = False
    '        '重置打印当前页
    '        _CurrentPage = 0
    '        '重置凭证列表索引
    '        _CurrentItemIndex = 0
    '    Else
    '        '还有
    '        e.HasMorePages = True
    '    End If

    'End Sub

    'Private Sub BeginPrintAcctSum(ByVal sender As Object, ByVal e As Printing.PrintEventArgs)
    '    '
    '    Debug.Print("开始打印：" & _CurrentPage)
    '    '重置打印变量
    '    _CurrentPage = 0

    '    '打印不能取消
    '    e.Cancel = False
    'End Sub

    'Private Sub EndPrintAcctSum(ByVal sender As Object, ByVal e As Printing.PrintEventArgs)

    '    If EndCallBack IsNot Nothing Then
    '        Dim ec As PrintCallBackEventArgs = New PrintCallBackEventArgs()
    '        ec.Msg = "打印完毕。"
    '        ec.PrintStatus = True

    '        EndCallBack.Invoke(Me, ec)

    '    End If
    'End Sub
#End Region

#Region "绘制文档部分"

    ''' <summary>
    ''' 绘制页面标题，即 科目汇总 
    ''' </summary>
    ''' <param name="e"></param>
    Private Sub DrawPageTitle(ByVal e As Printing.PrintPageEventArgs)

        '局部变量
        '文字部分的宽高
        Dim titleSize As SizeF = e.Graphics.MeasureString(m_PageTitle, m_TitleFont)

        'Dim titleWidth As Integer = e.Graphics.MeasureString(m_PageTitle, m_TitleFont).Width
        'Dim titleHeight As Integer = e.Graphics.MeasureString(m_PageTitle, m_TitleFont).Height
        '绘制凭证头部区

        '设置文字输出位置
        '水平方向居中输出
        Dim startX As Integer
        If e.PageBounds.Width > PageWidth Then
            startX = PageWidth / 2 - titleSize.Width / 2
        Else
            startX = e.PageBounds.Width / 2 - titleSize.Width / 2
        End If

        '垂直方向 在底部
        Dim startY As Integer = _CurrentPoint.Y + m_PageHeaderHeight - titleSize.Height - 5 '此处减5是为下线线留出位置

        '输出文字
        e.Graphics.DrawString(m_PageTitle, m_TitleFont, m_Brush, startX, startY)

        '标题下画线
        '标题长度
        Dim lineLen As Integer = titleSize.Width + 50 '下线划超过文字宽度50个单位（像素）
        Dim lineX As Integer
        If e.PageBounds.Width > PageWidth Then
            lineX = PageWidth / 2 - lineLen / 2
        Else
            lineX = e.PageBounds.Width / 2 - lineLen / 2
        End If

        '设置画线位置
        Dim lineStartP As Point = New Point(lineX, startY + titleSize.Height - 5)  '_CurrentPoint.Y + m_PageHeaderHeight - 5)
        Dim lineEndP As Point = New Point(lineX + lineLen, lineStartP.Y)
        '画第一条线
        e.Graphics.DrawLine(m_Pen, lineStartP, lineEndP)
        '向下移动3个单位
        lineStartP.Y += 2
        lineEndP.Y += 2
        '画第二条线
        e.Graphics.DrawLine(m_Pen, lineStartP, lineEndP)

        '坐标向下移动
        _CurrentPoint.X = LeftMargin
        _CurrentPoint.Y += m_PageHeaderHeight

    End Sub

    ''' <summary>
    ''' 绘制会计期间，在标题下，居中显示
    ''' </summary>
    ''' <param name="e"></param>
    Private Sub DrawWorkPeriod(ByVal e As Printing.PrintPageEventArgs)
        '会计期间信息居中显示 
        Dim tmpStr As String = "会计期间：" & CType(m_AcctSumList(_CurrentItemIndex), ClsTAccountSum).WorkPeriod
        Dim tmpDateWidth As Integer = e.Graphics.MeasureString(tmpStr, m_TextNormalFont).Width
        Dim x As Integer = PageWidth / 2 - tmpDateWidth / 2

        e.Graphics.DrawString(tmpStr, m_TextNormalFont, m_Brush, x, _CurrentPoint.Y)

        '坐标移动
        _CurrentPoint.X = LeftMargin
        _CurrentPoint.Y += m_WorkPeriodHeight

    End Sub

    ''' <summary>
    ''' 绘制单位信息
    ''' </summary>
    ''' <param name="e"></param>
    Private Sub DrawCompany(ByVal e As Printing.PrintPageEventArgs)
        '单位信息在最左侧底部
        Dim tmpStr As String = "核算单位：" & m_CompanyName
        Dim tmpSubStr As String = tmpStr
        Dim tmpNameSize As SizeF = e.Graphics.MeasureString(tmpStr, m_TextNormalFont)

        'Dim tmpNameWidth As Integer = e.Graphics.MeasureString(tmpStr, m_TextNormalFont).Width
        '判断宽度是否超过纸张,超出则截断
        If tmpNameSize.Width > RowWidth Then

            For i As Integer = 0 To tmpStr.Length
                tmpSubStr = tmpStr.Substring(0, i)
                If e.Graphics.MeasureString(tmpSubStr, m_TextNormalFont).Width > RowWidth Then
                    '此时截断
                    tmpSubStr = tmpStr.Substring(0, i - 1)
                    '
                    Exit For
                End If
            Next
        End If

        '输出位置计算
        Dim outPoint As Point = New Point(_CurrentPoint.X, _CurrentPoint.Y)
        outPoint.Y = _CurrentPoint.Y + m_CompanyHeight - tmpNameSize.Height

        '输出字串
        e.Graphics.DrawString(tmpSubStr, m_TextNormalFont, m_Brush, outPoint)

        '坐标移动
        _CurrentPoint.X = LeftMargin
        _CurrentPoint.Y += m_CompanyHeight
    End Sub

    ''' <summary>
    ''' 输出分录的文字部分
    ''' </summary>
    ''' <param name="e"></param>
    Private Sub DrawItemValue(ByVal e As Printing.PrintPageEventArgs, ByVal acctS As ClsTAccountSum)
        '输出 科目编码
        Dim acctCodeRect As Rectangle = New Rectangle()
        acctCodeRect.X = _CurrentPoint.X
        acctCodeRect.Y = _CurrentPoint.Y
        acctCodeRect.Width = m_AcctCodeWidth
        acctCodeRect.Height = m_ItemHeight '* e.Graphics.DpiY
        DrawCellValue(e, m_TextNormalFont, m_Brush, acctS.AcctCode, acctCodeRect, ContentAlignment.MiddleLeft)

        '输出 科目名称
        Dim acctNameRect As Rectangle = New Rectangle()
        acctNameRect.X = _CurrentPoint.X + m_AcctCodeWidth
        acctNameRect.Y = _CurrentPoint.Y
        acctNameRect.Width = m_AcctNameWidth
        acctNameRect.Height = m_ItemHeight
        DrawCellValue(e, m_TextNormalFont, m_Brush, acctS.AcctName, acctNameRect, ContentAlignment.MiddleLeft)

        '输出借方发生
        Dim debitRect As Rectangle = New Rectangle()
        debitRect.X = acctNameRect.X + m_AcctNameWidth
        debitRect.Y = _CurrentPoint.Y
        debitRect.Width = m_DebitWidth
        debitRect.Height = m_ItemHeight
        DrawCellValue(e, m_TextNormalFont, m_Brush, acctS.DebitSum.ToString("#,###.00"), debitRect, ContentAlignment.MiddleRight)

        '输出贷方发生
        Dim creditRect As Rectangle = New Rectangle()
        creditRect.X = debitRect.X + m_DebitWidth
        creditRect.Y = _CurrentPoint.Y
        creditRect.Width = m_CreditWidth
        creditRect.Height = m_ItemHeight
        DrawCellValue(e, m_TextNormalFont, m_Brush, acctS.CreditSum.ToString("#,###.00"), creditRect, ContentAlignment.MiddleRight)

    End Sub

    ''' <summary>
    ''' 画分录部分
    ''' </summary>
    ''' <param name="e"></param>
    Private Sub DrawItems(ByVal e As Printing.PrintPageEventArgs)

        '判断当前页可打印多少行
        Dim rowsInPage As Integer = _RowsOfPage
        '如果每页可打印最大行数多于当前记录总数，则取记录总数
        If rowsInPage > m_AcctSumList.Count Then
            rowsInPage = m_AcctSumList.Count
        End If
        '取出当前科目汇总的索引

        '输出记录内容,每页打印多少行
        For i As Integer = 0 To rowsInPage - 1
            '画分录表格线
            DrawItemLine(e, m_ItemHeight)
            '输出分录文字部分
            DrawItemValue(e, CType(m_AcctSumList(_CurrentItemIndex), ClsTAccountSum))

            '索引移动
            _CurrentItemIndex += 1

            '坐标移动
            _CurrentPoint.X = LeftMargin
            _CurrentPoint.Y += m_ItemHeight
        Next

    End Sub

    '''' <summary>
    '''' 绘制单元格内容
    '''' </summary>
    '''' <param name="e">打印页面参数</param>
    '''' <param name="v">打印的字串值</param>
    '''' <param name="rect">字串打印的矩形</param>
    '''' <param name="align">字串的对齐方式</param>
    'Private Sub DrawCellValue(ByVal e As Printing.PrintPageEventArgs, ByVal font As Font, ByVal brush As Brush, ByVal v As String, ByVal rect As Rectangle, ByVal align As ContentAlignment)
    '    '求字串宽高
    '    Dim strSizeF As SizeF = e.Graphics.MeasureString(v, font)
    '    '计算需要几行？
    '    Dim needLines As Integer = 1
    '    ' = IIf(rect.Width Mod strSizeF.Width = 0, rect.Width \ strSizeF.Width, rect.Width \ strSizeF.Width + 1)
    '    If strSizeF.Width > rect.Width Then
    '        '计算行数
    '        needLines = IIf(strSizeF.Width Mod rect.Width = 0, strSizeF.Width \ rect.Width, strSizeF.Width \ rect.Width + 1)
    '    End If
    '    '计算可容纳行数
    '    Dim allowLinews As Integer = rect.Height \ strSizeF.Height
    '    '判断可打印行数
    '    Dim lines As Integer = IIf(needLines > allowLinews, allowLinews, needLines)
    '    '打印数组
    '    Dim strs(lines - 1) As String

    '    Dim charIndex As Integer = 0
    '    For i As Integer = 0 To lines - 1
    '        strs(i) = ""
    '        For j As Integer = charIndex To v.Length - 1
    '            strs(i) &= v.Substring(j, 1)
    '            '判断长度是否达到了宽度值
    '            If e.Graphics.MeasureString(strs(i), font).Width > rect.Width Then
    '                '退回一个字符
    '                strs(i) = v.Substring(charIndex, strs(i).Length - 1)

    '                charIndex = j

    '                Exit For
    '            End If
    '        Next
    '    Next
    '    '开始打印输出
    '    Dim startX As Integer = rect.X
    '    Dim startY As Integer = rect.Y + (rect.Height - strSizeF.Height * lines) / 2 '第一行起始点,即纵向居中
    '    Dim outX As Single  '每行输出的X点
    '    Dim outY As Single  '每行输出的Y点

    '    For z As Integer = 0 To lines - 1
    '        'X计算要看对齐方式
    '        Select Case align
    '            Case ContentAlignment.MiddleCenter
    '                outX = startX + rect.Width / 2 - e.Graphics.MeasureString(strs(z), font).Width / 2
    '            Case ContentAlignment.MiddleLeft
    '                outX = startX
    '            Case ContentAlignment.MiddleRight
    '                outX = startX + rect.Width - e.Graphics.MeasureString(strs(z), font).Width
    '                '粗体会造成宽度不合适，需要手动增加
    '                If font.Bold Then
    '                    outX -= 2
    '                End If
    '            Case Else
    '                outX = startX
    '        End Select

    '        outY = startY + z * strSizeF.Height
    '        e.Graphics.DrawString(strs(z), font, brush, outX, outY)
    '    Next

    'End Sub

    ''' <summary>
    ''' 绘制表格线，表头线
    ''' </summary>
    ''' <param name="e"></param>
    ''' <param name="headerHeight"></param>
    Private Sub DrawItemLine(ByVal e As Printing.PrintPageEventArgs, ByVal headerHeight As Integer)
        '_CurrentPoint 坐标不变
        '画顶部线
        Dim rightTopPoint As Point = New Point()
        rightTopPoint.X = _CurrentPoint.X + RowWidth
        rightTopPoint.Y = _CurrentPoint.Y
        e.Graphics.DrawLine(m_Pen, _CurrentPoint, rightTopPoint)

        '画左侧线
        Dim leftBottomPoint As Point = New Point()
        leftBottomPoint.X = _CurrentPoint.X
        leftBottomPoint.Y = _CurrentPoint.Y + headerHeight
        e.Graphics.DrawLine(m_Pen, _CurrentPoint, leftBottomPoint)

        '画右侧线
        Dim rightBottomPoint As Point = New Point()
        rightBottomPoint.X = rightTopPoint.X
        rightBottomPoint.Y = leftBottomPoint.Y
        e.Graphics.DrawLine(m_Pen, rightTopPoint, rightBottomPoint)

        '画 科目编码右侧线
        Dim acctcodeRightTopPoint As Point = New Point()
        acctcodeRightTopPoint.X = _CurrentPoint.X + m_AcctCodeWidth
        acctcodeRightTopPoint.Y = _CurrentPoint.Y
        Dim acctcodeRightBottomPoint As Point = New Point()
        acctcodeRightBottomPoint.X = acctcodeRightTopPoint.X
        acctcodeRightBottomPoint.Y = acctcodeRightTopPoint.Y + headerHeight
        e.Graphics.DrawLine(m_Pen, acctcodeRightTopPoint, acctcodeRightBottomPoint)

        '画 科目名称右侧线
        Dim acctRightTopPoint As Point = New Point()
        acctRightTopPoint.X = acctcodeRightTopPoint.X + m_AcctNameWidth
        acctRightTopPoint.Y = _CurrentPoint.Y
        Dim acctRightBottomPoint As Point = New Point()
        acctRightBottomPoint.X = acctRightTopPoint.X
        acctRightBottomPoint.Y = acctRightTopPoint.Y + headerHeight
        e.Graphics.DrawLine(m_Pen, acctRightTopPoint, acctRightBottomPoint)

        '画 借方右侧线
        Dim debitRightTopPoint As Point = New Point()
        debitRightTopPoint.X = acctRightTopPoint.X + m_DebitWidth
        debitRightTopPoint.Y = _CurrentPoint.Y
        Dim debitRightBottomPoint As Point = New Point()
        debitRightBottomPoint.X = debitRightTopPoint.X
        debitRightBottomPoint.Y = _CurrentPoint.Y + headerHeight
        e.Graphics.DrawLine(m_Pen, debitRightTopPoint, debitRightBottomPoint)

        '画 贷方发生右侧线
        Dim creditRightTopPoint As Point = New Point()
        creditRightTopPoint.X = acctRightTopPoint.X + m_DebitWidth
        creditRightTopPoint.Y = _CurrentPoint.Y
        Dim creditRightBottomPoint As Point = New Point()
        creditRightBottomPoint.X = creditRightTopPoint.X
        creditRightBottomPoint.Y = creditRightTopPoint.Y + headerHeight
        e.Graphics.DrawLine(m_Pen, creditRightTopPoint, creditRightBottomPoint)

    End Sub

    ''' <summary>
    ''' 绘制表合计组
    ''' </summary>
    ''' <param name="e"></param>
    Private Sub DrawItemFooter(ByVal e As Printing.PrintPageEventArgs)
        '画表格线：左 上 右
        DrawItemLine(e, m_ItemHeight)

        '画下方表格线
        Dim bottomTopPoint As Point = New Point()
        bottomTopPoint.X = _CurrentPoint.X ' + m_RowWidth
        bottomTopPoint.Y = _CurrentPoint.Y + m_ItemHeight
        Dim bottomBottomPoint As Point = New Point()
        bottomBottomPoint.X = _CurrentPoint.X + RowWidth
        bottomBottomPoint.Y = _CurrentPoint.Y + m_ItemHeight
        e.Graphics.DrawLine(m_Pen, bottomTopPoint, bottomBottomPoint)

        '输出 合计
        Dim sumTextRect As Rectangle = New Rectangle()
        sumTextRect.X = _CurrentPoint.X
        sumTextRect.Y = _CurrentPoint.Y
        sumTextRect.Width = m_AcctCodeWidth
        sumTextRect.Height = m_ItemHeight
        DrawCellValue(e, m_TextBoldFont, m_Brush, "合计", sumTextRect, ContentAlignment.MiddleLeft)

        '输出借方发生
        Dim debitRect As Rectangle = New Rectangle()
        debitRect.X = _CurrentPoint.X + m_AcctCodeWidth + m_AcctNameWidth
        debitRect.Y = _CurrentPoint.Y
        debitRect.Width = m_DebitWidth
        debitRect.Height = m_ItemHeight
        DrawCellValue(e, m_TextBoldFont, m_Brush, m_DebitSum.ToString("#,#.00"), debitRect, ContentAlignment.MiddleRight)

        '输出 贷方发生
        Dim creditRect As Rectangle = New Rectangle()
        creditRect.X = debitRect.X + m_DebitWidth
        creditRect.Y = _CurrentPoint.Y
        creditRect.Width = m_CreditWidth
        creditRect.Height = m_ItemHeight
        DrawCellValue(e, m_TextBoldFont, m_Brush, m_CreditSum.ToString("#,#.00"), creditRect, ContentAlignment.MiddleRight)

        '坐标移动
        _CurrentPoint.X = LeftMargin
        _CurrentPoint.Y += m_ItemHeight

    End Sub

    ''' <summary>
    ''' 绘制表格头
    ''' </summary>
    ''' <param name="e"></param>
    Private Sub DrawItemHeader(ByVal e As Printing.PrintPageEventArgs)
        '画表格表头线
        DrawItemLine(e, m_ItemHeaderHeight)

        '输出 科目编码
        Dim acctCodeRect As Rectangle = New Rectangle()
        acctCodeRect.X = _CurrentPoint.X
        acctCodeRect.Y = _CurrentPoint.Y
        acctCodeRect.Width = m_AcctCodeWidth
        acctCodeRect.Height = m_ItemHeaderHeight
        DrawCellValue(e, m_HeaderFont, m_Brush, m_AcctCodeTitle, acctCodeRect, ContentAlignment.MiddleCenter)

        '输出 科目名称
        Dim acctNameRect As Rectangle = New Rectangle()
        acctNameRect.X = _CurrentPoint.X + m_AcctCodeWidth
        acctNameRect.Y = _CurrentPoint.Y
        acctNameRect.Width = m_AcctNameWidth
        acctNameRect.Height = m_ItemHeaderHeight
        DrawCellValue(e, m_HeaderFont, m_Brush, m_AcctNameTitle, acctNameRect, ContentAlignment.MiddleCenter)

        '输出 借方发生
        Dim debitRect As Rectangle = New Rectangle()
        debitRect.X = acctNameRect.X + m_AcctNameWidth
        debitRect.Y = _CurrentPoint.Y
        debitRect.Width = m_DebitWidth
        debitRect.Height = m_ItemHeaderHeight
        DrawCellValue(e, m_HeaderFont, m_Brush, m_DebitSumTitle, debitRect, ContentAlignment.MiddleCenter)

        '输出 借方发生
        Dim creditRect As Rectangle = New Rectangle()
        creditRect.X = debitRect.X + m_DebitWidth
        creditRect.Y = _CurrentPoint.Y
        creditRect.Width = m_CreditWidth
        creditRect.Height = m_ItemHeaderHeight
        DrawCellValue(e, m_HeaderFont, m_Brush, m_CreditSumTitle, creditRect, ContentAlignment.MiddleCenter)

        '打印光标下移
        _CurrentPoint.X = LeftMargin
        _CurrentPoint.Y += m_ItemHeaderHeight

    End Sub

    ''' <summary>
    ''' 绘制 页脚 
    ''' </summary>
    ''' <param name="e"></param>
    Protected Overrides Sub DrawPageFooter(ByVal e As Printing.PrintPageEventArgs)

        '画合计：
        DrawItemFooter(e)
        ''画凭证脚：人员信息
        'DrawPageFooter(e)

        '软件信息在最左侧

        '取软件名字
        Dim tmpStr As String = sysClsApp.AppName
        '最后输出的字串（是否截断的结果）
        Dim tmpSubStr As String = tmpStr
        '测量字串的宽高
        Dim tmpStrSize As SizeF = e.Graphics.MeasureString(tmpStr, m_FooterFont)

        '如果输出字串长度超过整个表格宽度，则截断字串
        If tmpStrSize.Width > RowWidth Then

            For i As Integer = 0 To tmpStr.Length
                tmpSubStr = tmpStr.Substring(0, i)
                If e.Graphics.MeasureString(tmpSubStr, m_FooterFont).Width > RowWidth Then
                    '此时截断
                    tmpSubStr = tmpStr.Substring(0, i - 1)
                    '
                    Exit For
                End If
            Next
        End If

        '计算页脚 高度，把文字输出 在底部
        Dim startPoint As Point = New Point(0, 0)
        startPoint.X = _CurrentPoint.X
        startPoint.Y = _CurrentPoint.Y + (m_PageFooterHeight - tmpStrSize.Height)
        '输出字串
        e.Graphics.DrawString(tmpSubStr, m_FooterFont, m_Brush, startPoint)
    End Sub


    Protected Overrides Sub DrawPageHeader(e As PrintPageEventArgs)
        'Throw New NotImplementedException()
        '画页标题
        DrawPageTitle(e)
        '画会计期间
        DrawWorkPeriod(e)
        '画公司名称
        DrawCompany(e)

    End Sub

    Protected Overrides Sub DrawPageBody(e As PrintPageEventArgs)
        'Throw New NotImplementedException()
        '如果是第一页，则记录索引为0
        '_CurrentItemIndex = (_CurrentPage - 1) * _RowsOfPage

        '画表头：
        DrawItemHeader(e)
        '画表格体：
        DrawItems(e)

    End Sub

    'Protected Overrides Sub DrawPageFooter(e As PrintPageEventArgs)
    '    Throw New NotImplementedException()
    'End Sub

#End Region
End Class
